package gui;

import code.*;

import interfaces.Order;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Date;

import javax.swing.*;

public class OfficeGui extends Applet {
	public Frame window;
	public Controller kontrol;
	public JLabel Oversigt;
	public JList liste;
	public JScrollPane sp_liste;
	public JButton opKnap;
	public JButton nedKnap;
	public JButton sletKnap;
	public JTextField id;
	public JTextField tegningNr;
	public JTextField dato;
	public JLabel Id_lable;
	public JLabel tegningNr_lable;
	public JLabel dato_lable;
	public JButton opretKnap;
	public DefaultListModel listModel;

	public static void main(String Args[]) {
		new OfficeGui();
	}

	public void init() {
		kontorGuiLayout customLayout = new kontorGuiLayout();

		setFont(new Font("Helvetica", Font.PLAIN, 12));
		setLayout(customLayout);

		Oversigt = new JLabel("Ordre Liste");
		add(Oversigt);

		listModel = new DefaultListModel();

		liste = new JList(listModel);
		liste.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		liste.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		liste.setVisibleRowCount(-1);
		for (Object obj : kontrol.orderList.getList()) {
			listModel.addElement(obj);
		}

		sp_liste = new JScrollPane(liste);
		add(sp_liste);

		opKnap = new JButton("Op");
		opKnap.addActionListener(new ButtonListener(this));
		add(opKnap);

		nedKnap = new JButton("Ned");
		nedKnap.addActionListener(new ButtonListener(this));
		add(nedKnap);

		sletKnap = new JButton("Slet");
		sletKnap.addActionListener(new ButtonListener(this));
		add(sletKnap);

		id = new JTextField("");
		add(id);

		tegningNr = new JTextField("");
		add(tegningNr);

		dato = new JTextField("");
		add(dato);

		Id_lable = new JLabel("Ordre Id:");
		add(Id_lable);

		tegningNr_lable = new JLabel("Tegning Nr:");
		add(tegningNr_lable);

		dato_lable = new JLabel("Leverings dato:");
		add(dato_lable);

		opretKnap = new JButton("Opret Ordre");
		opretKnap.addActionListener(new ButtonListener(this));
		add(opretKnap);

		setSize(getPreferredSize());

	}

	public OfficeGui() {
		window = new Frame("kontorGui");

		//TODO læs fra tekstfil i stedet
		kontrol = new Controller();
		kontrol.newOrder(1, 15, new Date(2010, 6, 21));
		kontrol.newOrder(2, 25, new Date(2010, 6, 22));
		kontrol.newOrder(3, 5, new Date(2010, 6, 24));
		kontrol.newOrder(4, 55, new Date(2010, 6, 26));
		kontrol.newOrder(5, 25, new Date(2010, 6, 27));
		kontrol.newOrder(6, 65, new Date(2010, 6, 22));

		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		init();
		window.add("Center", this);
		window.pack();
		window.setVisible(true);
	}
}

class kontorGuiLayout implements LayoutManager {

	public kontorGuiLayout() {
	}

	public void addLayoutComponent(String name, Component comp) {
	}

	public void removeLayoutComponent(Component comp) {
	}

	public Dimension preferredLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);

		Insets insets = parent.getInsets();
		dim.width = 700 + insets.left + insets.right;
		dim.height = 545 + insets.top + insets.bottom;

		return dim;
	}

	public Dimension minimumLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);
		return dim;
	}

	public void layoutContainer(Container parent) {
		Insets insets = parent.getInsets();

		Component c;
		c = parent.getComponent(0);
		if (c.isVisible()) {
			c.setBounds(insets.left + 19, insets.top + 13, 390, 26);
		}
		c = parent.getComponent(1);
		if (c.isVisible()) {
			c.setBounds(insets.left + 19, insets.top + 51, 390, 461);
		}
		c = parent.getComponent(2);
		if (c.isVisible()) {
			c.setBounds(insets.left + 422, insets.top + 51, 58, 19);
		}
		c = parent.getComponent(3);
		if (c.isVisible()) {
			c.setBounds(insets.left + 422, insets.top + 83, 58, 19);
		}
		c = parent.getComponent(4);
		if (c.isVisible()) {
			c.setBounds(insets.left + 422, insets.top + 128, 58, 19);
		}
		c = parent.getComponent(5);
		if (c.isVisible()) {
			c.setBounds(insets.left + 550, insets.top + 294, 141, 19);
		}
		c = parent.getComponent(6);
		if (c.isVisible()) {
			c.setBounds(insets.left + 550, insets.top + 326, 141, 19);
		}
		c = parent.getComponent(7);
		if (c.isVisible()) {
			c.setBounds(insets.left + 550, insets.top + 358, 141, 19);
		}
		c = parent.getComponent(8);
		if (c.isVisible()) {
			c.setBounds(insets.left + 442, insets.top + 294, 96, 19);
		}
		c = parent.getComponent(9);
		if (c.isVisible()) {
			c.setBounds(insets.left + 442, insets.top + 326, 96, 19);
		}
		c = parent.getComponent(10);
		if (c.isVisible()) {
			c.setBounds(insets.left + 442, insets.top + 358, 96, 19);
		}
		c = parent.getComponent(11);
		if (c.isVisible()) {
			c.setBounds(insets.left + 442, insets.top + 403, 160, 26);
		}
	}
}

class ButtonListener implements ActionListener {
	OfficeGui gui;

	ButtonListener(OfficeGui gui) {
		this.gui = gui;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Op")) {
			System.out.println("opKnap has been clicked, "
					+ gui.liste.getSelectedIndex());
			if (gui.liste.getSelectedIndex() > 0) {
				Order change = gui.kontrol.orderList.getItem(gui.liste
						.getSelectedIndex());
				gui.kontrol.changePriority(change,
						(gui.liste.getSelectedIndex() - 1));
			}
			refresh();
		} else if (e.getActionCommand().equals("Ned")) {
			System.out.println("nedKnap has been clicked, "
					+ gui.liste.getSelectedIndex());
			if (gui.liste.getSelectedIndex() < gui.kontrol.orderList.getList()
					.size()) {
				Order change = gui.kontrol.orderList.getItem(gui.liste
						.getSelectedIndex());
				gui.kontrol.changePriority(change,
						(gui.liste.getSelectedIndex() + 1));
			}
			refresh();
		} else if (e.getActionCommand().equals("Slet")) {
			System.out.println("SletKnap has been clicked");
			int index = gui.liste.getSelectedIndex();
			if (index != -1)
				gui.kontrol.completeOrder(gui.kontrol.orderList.getList()
						.get(index).getId());
			refresh();
		} else if (e.getActionCommand().equals("Opret Ordre")) {
			System.out.println("Opret Ordre Knap has been clicked");
			String ID = gui.id.getText();
			String TEGNING = gui.tegningNr.getText();
			String DATO = gui.dato.getText();
			if (!ID.equals("") && !TEGNING.equals("") && !DATO.equals("")) {
				int id = Integer.parseInt(ID);
				Boolean duplicate = false;
				for (Order odr : gui.kontrol.orderList.getList()) {
					System.out.println(odr.getId() + " " + id);
					if (odr.getId() == id)
						duplicate = true;
				}
				if (!duplicate) {
					int tegning = Integer.parseInt(TEGNING);
					String datoData[] = DATO.split("/");
					int date = Integer.parseInt(datoData[0]);
					datoData = datoData[1].split("-");
					int month = Integer.parseInt(datoData[0]);
					int year = Integer.parseInt(datoData[1]);
					Date dato = new Date(date, month, year);
					gui.kontrol.newOrder(id, tegning, dato);
					gui.id.setText("");
					gui.tegningNr.setText("");
					gui.dato.setText("");
				}
			}
			refresh();
		}
	}

	public void refresh() {
		gui.listModel.removeAllElements();
		for (Object obj : gui.kontrol.orderList.getList()) {
			gui.listModel.addElement(obj);
		}
		//TODO skriv til fil
	}
}